package com.ssly.weixin.xichang.admin.controller.api;

import com.ssly.weixin.xichang.common.domain.Report;
import com.ssly.weixin.xichang.common.repository.ReportRepository;
import com.ssly.weixin.xichang.util.JsonResult;
import com.ssly.weixin.xichang.util.R;
import com.ssly.weixin.xichang.util.SMS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Optional;


@RestController
@RequestMapping("/admin/api/report")
public class ReportController {
    @Autowired
    private ReportRepository reportRepository;
    @GetMapping
    public DataTablesOutput<Report> index(DataTablesInput input) {
        return reportRepository.findAll(input);
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        reportRepository.deleteById(id);
        return R.success("删除成功");
    }
    @PostMapping("/{id}")
    public JsonResult allow(@PathVariable Integer id,@RequestParam("action") Integer action) {
        Report report = reportRepository.findById(id).get();
        report.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        report.setStatus(action);
        String phone=report.getPhone();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
        String date = simpleDateFormat.format(report.getCreateTime());
        reportRepository.save(report);
        SMS sms=new SMS();
        if (action==1){
            String content = "您好，您于"+date+"在廉洁西昌提交的举报内容已通过审核！";
            sms.send(phone, content);
            return R.success("核审成功");
        }
        if(action==-1){
            String content = "您好，您于"+date+"在廉洁西昌提交的举报内容已被驳回！";
            sms.send(phone, content);
            return R.success("驳回成功");
        }
        return R.error("非法操作");
    }

}
